rm(list = ls(all.names = TRUE))

library(foreign)
library(dplyr)
library(readstata13)
library(ggplot2)
library(arm)


Data <- read.dta13("~/Dropbox/Mingling for IO/ICC paper data/IO Replication PUTNAM/Layering_Data_Oct_2019 replication.dta")
Data <- subset(Data, year == 2006)

#sort(names(Data)) 

#View(Data)

#dates_ratified <- read.csv("/Users/carafeldkamp/Documents/ICC_ratified.csv")
#Data <- merge(Data, dates_ratified, by="country", all.x=TRUE)


Data$pactdummy <-NA
Data[Data$defpact==0 & !is.na(Data$defpact),]$pactdummy <- "No"
Data[Data$defpact==1 & !is.na(Data$defpact),]$pactdummy <- "Yes"

Data$gspdummy <-NA
Data[Data$gspstatus==0 & !is.na(Data$gspstatus),]$gspdummy <- "No"
Data[Data$gspstatus==1 & !is.na(Data$gspstatus),]$gspdummy <- "Yes"



######################################
###Number of countries by Assurance Score
#Area Plot 2006

#to get straight drop down of lines
Data$joindummy <- 0
Data[Data$earlyrat==1 & Data$iccmember==1,]$joindummy <- 1 #Early joiners
Data[Data$earlyrat==0 & Data$iccmember==1,]$joindummy <- 2 #Late ICC joiners
Data[Data$iccmember==0,]$joindummy <-3 #Non-joiners

country_count <- data.frame(Data %>% group_by(exposure4, joindummy) %>% summarize(n()))
colnames(country_count) <- c("exposure4", "joindummy","score_count")

#I added in rows where there were 0s, which eliminates gaps in the area graph
#country_count <- add_row(country_count,exposure4 = -3,joindummy = 2,score_count = 0)
#country_count <- add_row(country_count,exposure4 = -3,joindummy = 3,score_count = 0)
#country_count <- add_row(country_count,exposure4 = -2,joindummy = 3,score_count = 0)
#country_count <- add_row(country_count,exposure4 = -1,joindummy = 3,score_count = 0)
#country_count <- add_row(country_count,exposure4 = 6,joindummy = 1,score_count = 0)
#country_count <- add_row(country_count,exposure4 = 6,joindummy = 2,score_count = 0)
#country_count <- add_row(country_count,exposure4 = 7,joindummy = 1,score_count = 0)
#country_count <- add_row(country_count,exposure4 = 7,joindummy = 2,score_count = 0)
#country_count <- add_row(country_count,exposure4 = 8,joindummy = 1,score_count = 0)
#country_count <- add_row(country_count,exposure4 = 8,joindummy = 2,score_count = 0)

country_count$joinwords <- NA
country_count[country_count$joindummy==1,]$joinwords <- "Early Joiners"
country_count[country_count$joindummy==2,]$joinwords <- "Late Joiners"
country_count[country_count$joindummy==3,]$joinwords <- "Non-Joiners"

g4 <- ggplot(data=country_count,
             aes(x=exposure4,
                 y=score_count,
                 fill=joinwords
                 #color=joinwords
             ))
#g4 <- g4 + geom_line(size=1, aes(linetype=joinwords)) 
g4 <- g4 + geom_point(size=1,alpha=.8 #,position=position_jitter(h=0.1,w=0.1)
)
g4 <- g4 + geom_area(position="identity",alpha=.5, size=.2, color="black")
g4 <- g4 + scale_x_continuous(breaks=c(-3,-2,-1,0,1,2,3,4,5,6,7,8))
g4 <- g4 + theme_minimal() 
g4 <- g4 + scale_fill_manual(values = c("lightgrey", "black","white"))
#g4 <- g4 + scale_color_manual(values = c("#C0C0C0", "black","#707070"))
#g4 <- g4 + scale_linetype_manual(values=c("solid", "dotted", "dashed"))
g4 <- g4 + guides(size=FALSE, color=FALSE, linetype=FALSE, point=FALSE)
g4 <- g4 + labs(x="Assurance Score",
                y="Number of Countries",
                title="Early ICC Joiners, Late Joiners, and Non-Joiners in 2006",
                #shape="ICC Join Date"
                fill="ICC Join Date"
)
g4 <- g4 + theme(legend.position="bottom",
                 axis.text.x = element_text(size=12, face="bold"),
                 axis.text.y = element_text(size=12, face="bold"),
                 axis.line.y = element_line(colour = "grey", size = .5,linetype = "solid"),
                 axis.line.x = element_line(colour = "grey", size = .5,linetype = "solid"),
                 plot.title = element_text(family = "sans", size = 14, margin=margin(0,0,10,0),face="bold", hjust=0.5),
                 panel.grid.minor.x=element_blank(),
                 panel.grid.minor.y=element_blank(),
                 axis.title.x = element_text(size=14,face="bold"),
                 axis.title.y = element_text(size=14,face="bold"))

pdf("descriptive_2006.pdf")
plot(g4)
dev.off()
# 
# 
